/*
 * @lc app=leetcode.cn id=1763 lang=typescript
 *
 * [1763] 最长的美好子字符串
 */

// @lc code=start
function longestNiceSubstring(s: string): string {
    const isCorrect = (str: string) => {
        const temp = new Array(26).fill(0);
        const temp1 = new Array(26).fill(0);
        for (let i = 0; i < str.length; i++) {
            const element = str[i];
            const code = element.charCodeAt(0);
            if (code < 96) {
                temp[code - 65]++;
            } else {
                temp1[code - 97]++;
            }
        }

        return temp.every((i, index) => {
            if (i === 0 && temp1[index] === 0) {
                return true;
            }
            if (i > 0 && temp1[index] > 0) {
                return true;
            }
            return false;
        });
    };

    let arr: string[] = [];
    for (let i = 0; i < s.length; i++) {
        for (let j = i + 1; j < s.length; j++) {
            const it = s.slice(i, j + 1);
            if (isCorrect(it)) arr.push(it);
        }
    }
    return arr.reduce((col, cur) => {
        return cur.length > col.length ? cur : col;
    }, "");
}
// @lc code=end
